/////////////////////////////////////////////////////////////////////////////
//
// (c) 2007 BinaryComponents Ltd.  All Rights Reserved.
//
// http://www.binarycomponents.com/
//
/////////////////////////////////////////////////////////////////////////////

using System;
using System.Drawing;
using System.Windows.Forms;
using System.Windows.Forms.VisualStyles;
using SuperListEx.Utility.Collections;

namespace SuperListEx.Sections
{
  public class HeaderSection : HeaderColumnSectionContainer
  {
    #region vars
    private bool _isVisible = true;
    #endregion

    #region properties
    public bool IsVisible
    {
      get
      {
        return _isVisible;
      }
      set
      {
        if (_isVisible != value)
        {
          _isVisible = value;
          Host.LazyLayout(Parent);
        }
      }
    }

    public int IdealWidth
    {
      get
      {
        int total = 0;
        foreach (Section s in Children)
          total += s.Rectangle.Width;
        return total;
      }
    }
    #endregion

    #region constructor
    public HeaderSection(ISectionHost hostControl, EventingList<Column> columns)
      : base(hostControl, columns)
    {
      Columns.DataChanged += Columns_DataChanged;
    }
    #endregion

    #region public functions
    public override void SetColumnSortOrder(Column column, SortOrder sortOrder)
    {
      foreach (Column c in Columns)
      {
        if (c != column)
        {
          c.SortOrder = SortOrder.None;
        }
      }
      column.SortOrder = sortOrder;
      Invalidate();
    }

    public override void Layout(GraphicsSettings settings, Size maximumSize)
    {
      int width = 0;
      int height = 0;

      SyncSectionsToColumns(DisplayMode.Header);

      Point pt = new Point(Location.X, Location.Y);

      int reservedSpace = LayoutController == null ? 0 : LayoutController.ReservedNearSpace;

      bool first = true;

      foreach (Section hcs in Children)
      {
        hcs.Location = pt;
        hcs.Layout(settings, new Size(maximumSize.Width - pt.X, _isVisible ? maximumSize.Height - pt.Y : 0));
        if (first)
        {
          hcs.Size = new Size(hcs.Size.Width + reservedSpace, hcs.Size.Height);
          first = false;
        }
        pt = new Point(hcs.Rectangle.Right, pt.Y);
        width += hcs.Rectangle.Width;
        if (hcs.Size.Height > height)
        {
          height = hcs.Size.Height;
        }
      }
      if (height == 0 && _isVisible)
      {
        if (VisualStyleRenderer.IsSupported)
        {
          VisualStyleRenderer renderer = new VisualStyleRenderer(VisualStyleElement.Header.Item.Normal);

          height = renderer.GetPartSize(settings.Graphics, ThemeSizeType.True).Height;
        }
        else
        {
          height = SystemFonts.DialogFont.Height + 6;
        }
      }
      if (height < MinimumHeight)
      {
        height = MinimumHeight;
      }
      Size = new Size(Math.Max(maximumSize.Width, width), height);
      if (LayoutController != null)
      {
        LayoutController.HeaderLayedOut();
      }
    }

    public override void PaintBackground(GraphicsSettings gs, Rectangle clipRect)
    {
      Rectangle rc = Rectangle;

      rc.Width += 2; // hide end vert bars

      if (VisualStyleRenderer.IsSupported)
      {
        VisualStyleRenderer vsr = new VisualStyleRenderer(VisualStyleElement.Header.Item.Normal);

        vsr.DrawBackground(gs.Graphics, rc);
      }
      else
      {
        gs.Graphics.FillRectangle(SystemBrushes.Control, rc);
        ControlPaint.DrawBorder3D(gs.Graphics, rc, Border3DStyle.Raised);
      }

      base.Paint(gs, clipRect);
    }
    #endregion

    private void Columns_DataChanged(object sender, EventInfo<SuperListEx.Column> e)
    {
      Host.LazyLayout(Parent);
    }
  }
}